package com.fq.myapplication.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.recyclerview.widget.RecyclerView;
import com.fq.myapplication.R;
import com.fq.myapplication.activity.SettingsActivity;
import com.fq.myapplication.bean.SettingItem;
import com.fq.myapplication.utils.ThemeManager;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Switch;
public class SettingsAdapter extends RecyclerView.Adapter<SettingsAdapter.ViewHolder> {
    private List<SettingItem> items;
    private Context context;

    public SettingsAdapter(List<SettingItem> items) {
        this.items = items;
    }

    @Override
    public int getItemViewType(int position) {
        return items.get(position).getType();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view;
        switch (viewType) {
            case SettingItem.TYPE_GROUP_TITLE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_settings_group, parent, false);
                return new GroupTitleViewHolder(view);
            case SettingItem.TYPE_NORMAL:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_settings_normal,parent, false);
                return new NormalViewHolder(view);
            case SettingItem.TYPE_SWITCH:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_settings_switch,parent, false);
                return new SwitchViewHolder(view);
            case SettingItem.TYPE_BUTTON:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_settings_button, parent, false);
                return new ButtonViewHolder(view);
            default:
                throw new IllegalArgumentException("Invalid view type");
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SettingItem item = items.get(position);
        if (holder instanceof GroupTitleViewHolder) {
            ((GroupTitleViewHolder) holder).title.setText(item.getTitle());
        } else if (holder instanceof NormalViewHolder) {
            ((NormalViewHolder) holder).title.setText(item.getTitle());
            ((NormalViewHolder) holder).icon.setImageResource(item.getIconRes());
            if (item.getDesc() != null) {
                ((NormalViewHolder) holder).desc.setText(item.getDesc());
                ((NormalViewHolder) holder).desc.setVisibility(View.VISIBLE);
            } else {
                ((NormalViewHolder) holder).desc.setVisibility(View.GONE);
            }
        } else if (holder instanceof SwitchViewHolder) {
            ((SwitchViewHolder) holder).title.setText(item.getTitle());
            ((SwitchViewHolder) holder).icon.setImageResource(item.getIconRes());
            ((SwitchViewHolder) holder).switchButton.setChecked(item.isChecked());
            ((SwitchViewHolder) holder).switchButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
                item.setChecked(isChecked);
                if ("夜间模式".equals(item.getTitle())) {
                    int mode = isChecked ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO;
                    ThemeManager.saveThemeMode(context, mode);
                    ((SettingsActivity) context).updateTheme(mode);
                }
            });
        } else if (holder instanceof ButtonViewHolder) {
            ((ButtonViewHolder) holder).button.setText(item.getTitle());
        }
    }


    @Override
    public int getItemCount() {
        return items.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
    public static class GroupTitleViewHolder extends ViewHolder {
        TextView title;

        public GroupTitleViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
        }
    }
    public static class NormalViewHolder extends ViewHolder {
        TextView title;
        TextView desc;
        ImageView icon;

        public NormalViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            desc = itemView.findViewById(R.id.desc);
            icon = itemView.findViewById(R.id.icon);
        }
    }

    public static class SwitchViewHolder extends ViewHolder {
        TextView title;
        TextView desc;
        ImageView icon;
        Switch switchButton;

        public SwitchViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            desc = itemView.findViewById(R.id.desc);
            icon = itemView.findViewById(R.id.icon);
            switchButton = itemView.findViewById(R.id.switch_button);
        }
    }


    public static class ButtonViewHolder extends ViewHolder {
        TextView button;

        public ButtonViewHolder(View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.button);
        }
    }

}
